#include "TopbarWidget.h"
#include <QApplication>
#include <QPainter>
#include <QFont>
#include <QDebug>
#include <QStyleOption>
#include <QStyle>

TopbarWidget::TopbarWidget(QWidget *parent)
    : QWidget(parent)
    , m_mainLayout(nullptr)
    , m_leftWidget(nullptr)
    , m_logoLabel(nullptr)
    , m_rightWidget(nullptr)
    , m_photoLabel(nullptr)
    , m_userLabel(nullptr)
    , m_separatorLabel(nullptr)
    , m_stoveLabel(nullptr)
    , m_exitButton(nullptr)
    , m_titleText("")
{   
    setupUI();
    setupConnections();
}

TopbarWidget::~TopbarWidget()
{
}

void TopbarWidget::setupUI()
{
    // 设置固定高度
    setFixedHeight(80);
    
    // 主布局
    m_mainLayout = new QHBoxLayout(this);
    m_mainLayout->setContentsMargins(42, 0, 42, 0);
    m_mainLayout->setSpacing(0);

    // 左侧布局
    m_leftWidget = new QWidget(this);
    QHBoxLayout *leftLayout = new QHBoxLayout(m_leftWidget);
    leftLayout->setContentsMargins(0, 0, 0, 0);
    leftLayout->setSpacing(0);
    m_mainLayout->addWidget(m_leftWidget);

    m_mainLayout->addStretch();

    // logo
    m_logoLabel = new QLabel(this);
    m_logoLabel->setObjectName("logoLabel");
    m_logoLabel->setFixedSize(24, 24);
    m_logoLabel->setScaledContents(true);
    leftLayout->addWidget(m_logoLabel);

    // 右侧布局
    m_rightWidget = new QWidget(this);
    QHBoxLayout *rightLayout = new QHBoxLayout(m_rightWidget);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->setSpacing(8);
    m_mainLayout->addWidget(m_rightWidget);
    
    // 用户头像
    m_photoLabel = new QLabel(m_rightWidget);
    m_photoLabel->setStyleSheet("QLabel { background-color: transparent; border-radius: 12px; }");
    m_photoLabel->setFixedSize(24, 24);
    m_photoLabel->setScaledContents(true);
    
    // 用户信息
    m_userLabel = new QLabel("", m_rightWidget);
    m_userLabel->setProperty("label14", true);

    // 分割线
    m_separatorLabel = new QLabel("|", m_rightWidget);
    m_separatorLabel->setProperty("label14", true);
    m_separatorLabel->setVisible(false);

    // 炉子
    m_stoveLabel = new QLabel("", m_rightWidget);
    m_stoveLabel->setProperty("label14", true);
    
    // 退出按钮
    m_exitButton = new QPushButton("退出", m_rightWidget);
    m_exitButton->setObjectName("exitButton");
    
    rightLayout->addWidget(m_photoLabel);
    rightLayout->addWidget(m_userLabel);
    rightLayout->addWidget(m_separatorLabel);
    rightLayout->addWidget(m_stoveLabel);
    rightLayout->addWidget(m_exitButton);
}

void TopbarWidget::setupConnections()
{
    connect(m_exitButton, &QPushButton::clicked, this, &TopbarWidget::exitRequested);
}

void TopbarWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    
    // 绘制标题文字
    if (!m_titleText.isEmpty()) {
        // 保存当前painter状态
        painter.save();
        
        // 设置字体：大小32，字重bold
        QFont titleFont("Microsoft YaHei", 32, QFont::Bold);
        painter.setFont(titleFont);
        
        // 设置渐变色：#FFFFFF-#45BFFF
        QLinearGradient gradient(0, 0, width(), 0);
        gradient.setColorAt(0, QColor(255, 255, 255));      // #FFFFFF 白色
        gradient.setColorAt(1, QColor(69, 191, 255));       // #45BFFF 蓝色
        
        // 计算标题位置（完全居中）
        QFontMetrics fm(titleFont);
        QRect textRect = fm.boundingRect(m_titleText);
        
        // 在整个widget中居中绘制标题
        int textX = (width() - textRect.width()) / 2;
        int textY = (height() + textRect.height()) / 2 - fm.descent();
        
        // 绘制文字投影：#002A57，向下2px
        painter.setPen(QColor(0, 42, 87)); // #002A57 深蓝色
        painter.drawText(textX, textY + 2, m_titleText); // 向下偏移2px
        
        // 绘制主文字（渐变效果）
        painter.setPen(QPen(gradient, 1));
        painter.drawText(textX, textY, m_titleText);
        
        // 恢复painter状态
        painter.restore();
    }
    
    QWidget::paintEvent(event);
}

void TopbarWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    update(); // 触发重绘以更新背景
}

void TopbarWidget::setLogo(const QString &logo)
{
    m_logoLabel->setPixmap(QPixmap(logo).scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void TopbarWidget::setTitle(const QString &title)
{
    m_titleText = title;
    update(); // 触发重绘
}

QString TopbarWidget::getTitle() const
{
    return m_titleText;
}

void TopbarWidget::setPhoto(const QString &photo)
{
    m_photoLabel->setPixmap(QPixmap(photo).scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void TopbarWidget::setUserInfo(const QString &userInfo)
{
    m_userLabel->setText(userInfo);
}

QString TopbarWidget::getUserInfo() const
{
    return m_userLabel->text();
}

void TopbarWidget::setStoveInfo(const QString &stoveInfo)
{
    m_stoveLabel->setText(stoveInfo);
}

QString TopbarWidget::getStoveInfo() const
{
    return m_stoveLabel->text();
}

void TopbarWidget::showSeparator(bool show)
{
    m_separatorLabel->setVisible(show);
}
